<?php
require_once (APPLICATION_DIR."/library/external/XOAD/xoad.php");
require_once(APPLICATION_DIR."/library/external/fckeditor/fckeditor.php") ;
class AdminalumnicareerController extends DRX_Controller
{
	function xoadGetMeta()
	{
		XOAD_Client::mapMethods($this,array("listjob", "addcareer", "docareer", "editcareer", "deletecareer", "viewcareer"));
	}
	
	function viewAction()
	{
		global $arrArgs;
		$objSmarty = DRX::load('smarty');
		$this -> prepareController();
		$objCareer = new AlumniCareer();
		
		if ( !$arrInfoCareer = $objCareer -> getInfoCareer($arrArgs[2]) )
			return "INVALID";
		
		$objSmarty -> assign("arrInfoCareer", $arrInfoCareer);
		
		$objSmarty -> assign("content_action", $objSmarty -> fetch("admin/alumni/career/view_career.tpl"));
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/career/index.tpl"));
		$objSmarty->display("admin/admin.tpl");
		
	}
	
	function deletecareer($p_arrArgs)
	{
		$objCareer = new AlumniCareer();
		if ( $objCareer -> deleteCareer($p_arrArgs["career_id"]) )
			return "SUCCESS";
		else
			return "FAIL";
	}
	
	function editAction()
	{
		global $arrArgs;
		$objSmarty = DRX::load('smarty');
		$objCareer = new AlumniCareer();
		$objGeneral = new AlumniGeneral();
		$this -> prepareController();
		
		if ( !$arrInfoCareer = $objCareer -> getInfoCareer($arrArgs[2]) )
			return "INVALID";
		
		if ( isset($_POST['position']) )
		{
			$this -> docareer();
			redirect(APPLICATION_URL . "admin/alumni/career/list");
		}
		
		$arrLocation = $objGeneral -> getListLocation();
		$objSmarty -> assign("arrLocation", $arrLocation);
		
		$arrIndustry = $objGeneral -> getListIndustry();
		$objSmarty -> assign("arrIndustry", $arrIndustry);
		
		$objSmarty -> assign("arrInfoCareer", $arrInfoCareer);
		$objSmarty -> assign("fck_editor", $this -> createFCK("description", $arrInfoCareer["description"]));
		
		$objSmarty -> assign("content_action", $objSmarty -> fetch("admin/alumni/career/frm_edit.tpl"));
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/career/index.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function docareer()
	{
		$arrForm = $_POST;
		$objCareer = new AlumniCareer();
		
		if ( $arrForm["action"] == "add" )
		{
			if ( $objCareer -> addJob($arrForm) )
				return true;
			else	
				return false;
		}
		elseif ( $arrForm["action"] == "update" )
		{
			if ( $objCareer -> updateJob($arrForm) )
				return true;
			else
				return false;
		}
	}
	
	function addAction()
	{
		$objSmarty = DRX::load('smarty');
		$objCareer = new AlumniCareer();
		$objGeneral = new AlumniGeneral();
		$this -> prepareController();
		
		if ( $_POST['position'] )
		{
			$this -> docareer();
			redirect(APPLICATION_URL . "admin/alumni/career/list");
		}
		
		$arrLocation = $objGeneral -> getListLocation();
		$objSmarty -> assign("arrLocation", $arrLocation);
		
		$arrIndustry = $objGeneral -> getListIndustry();
		$objSmarty -> assign("arrIndustry", $arrIndustry);
		$objSmarty -> assign("fck_editor", $this -> createFCK("description"));
		
		$objSmarty -> assign("content_action", $objSmarty -> fetch("admin/alumni/career/frm_add.tpl"));
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/career/index.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function listAction()
	{
		global $arrArgs;
		$objSmarty = DRX::load('smarty');
		$this -> prepareController();
		$objCareer = new AlumniCareer();
		
		//echo '<pre>';var_dump($arrArgs);
		
		define("RESULT_PER_PAGE_CAREER", 2);
		$intPage = isset($arrArgs[2]) ? $arrArgs[2] : 1;
		$intStart = ($intPage-1)*RESULT_PER_PAGE_CAREER;
		$objListCareer = $objCareer -> getListCareer($intStart, RESULT_PER_PAGE_CAREER);
		
		//phan trang
		include "IO_Page.php";
		$intTotal= $objListCareer -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULT_PER_PAGE_CAREER, 'listJob');
		$strNaviPage = $objNaviPage -> createPageNavigator($intPage, $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		
		$objSmarty -> assign("objListCareer", $objListCareer);
		$objSmarty -> assign("intStart", $intStart);
		
		$objSmarty -> assign("content_action", $objSmarty -> fetch("admin/alumni/career/list_career.tpl") );
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/career/index.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function prepareController()
	{
		$this -> runXoadServer();
		$this -> pathWeb();
		$this -> getLeftMenu();
	}
		
	function indexAction()
	{
		$this -> listAction();		
	}
	
	function getLeftMenu()
	{
		$objSmarty = DRX::load('smarty');		
		$objSmarty -> assign("leftmenu", $objSmarty -> fetch("admin/alumni/career/left_menu.tpl"));
	}
	
	function createFCK($p_strName, $p_strValue = "")
	{
		$oFCKeditor = new FCKeditor($p_strName) ;
		$oFCKeditor->BasePath = FCK_BASE_PATH;
		$oFCKeditor->Height = FCK_DEFAULT_HEIGHT;
		$oFCKeditor->Value = $p_strValue;
		$fck = $oFCKeditor->Create();
		return $fck;
	}
	
	function pathWeb()
	{
		$objSmarty = DRX::load('smarty');
		$list_path = array();
		$prefix_url             = APPLICATION_URL . ADMIN_URL;        
    $list_path[0]['url']    = $prefix_url;
    $list_path[0]['title']  = ADMIN_HOME_PATH_WEB_TITLE;
		$last_path['url']    = $prefix_url . "/alumni/career";
    $last_path['title']  = "Alumni Career";
		
		$objSmarty -> assign("list_path",$list_path);
		$objSmarty -> assign("last_path",$last_path);
		$path_web = $objSmarty->fetch("admin/path_web.tpl");
	  $objSmarty -> assign("path_web",$path_web);
	}
	
	function runXoadServer()
	{
		$objSmarty = DRX::load('smarty');
		
		if ( XOAD_Server::runServer() )
		{
			exit();
		}
		
		$objSmarty -> assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL . "library/external/XOAD"));	
		$objSmarty -> assign("xoad_handler", XOAD_Client::register($this, "/Adminalumnicareer"));
	}	
}
?>